package cn.youyeku.zsxh.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.AppConfig;
import cn.youyeku.zsxh.Utils.CircleImageView;
import cn.youyeku.zsxh.Utils.ImageUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.question_data;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.fragment.ABS.AbsSwipeFragment;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-3-26.
 */

public class FgmLibs extends AbsSwipeFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initView() {

    }

    public FgmLibs() {
        super(R.layout.fgm_base_refresh_layout, R.id.fgm_base_recycle, R.id.fgm_base_swipe);
    }

    @Override
    protected RefreshFootAdapter getAapter() {
        return new RefreshFootAdapter<question_data>(R.layout.fgm_question_item,R.layout.refresh_footer) {


            @Override
            protected void onBindCustomerData(RefreshViewHolder holder, final question_data data, int pos) {

                CircleImageView img = holder.getChildView(R.id.question_img);
                TextView nick_name = holder.getChildView(R.id.question_nick_name);
                TextView test_desc  = holder.getChildView(R.id.question_text_desc);

                ImageUtils.GlideCircleImg(getContext(),img, XhApi.getFace(data.getTeacher_uid()));
                nick_name.setText(data.getNick_name());
                test_desc.setText(data.getQuestion_desc());
                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showUserInfo(getContext(), data.getTeacher_uid());
                    }
                });

            }

            @Override
            protected void setmOnItemClick(View itemView, final question_data data, int pos) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                            UIControl.showQuestionDoActivity(getContext(),data);


                    }
                });
            }
        };
    }

    @Override
    protected void requestData() {


        XhApi.get_question(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int code = object.getInt("code");

                    LogUtils.w("code  == " + code + object.getString("message"));

                    if(code ==200){
                        Gson gson = new Gson();
                        mData = gson.fromJson(object.getString("data"), new TypeToken<List<question_data>>() {
                        }.getType());
                    }
                    Message msg = new Message();
                    msg.what = code;
                    handler.sendMessage(msg);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
